package com.study.custom.mybatis;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @desc:
 * @author: panxiyi
 * @date: 2023/12/23 23:08
 */

/**
 * 创建代理对象
 */
public class MapperProxyFactory implements InvocationHandler {

    private SqlSession sqlSession;
    public MapperProxyFactory(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }


    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("--------do invoke enter" + method);
        // 生成代理对象
        String className = method.getDeclaringClass().getName();
        String methodName = method.getName();   //方法名字，和UserMapper.xml中的id值一致
        String key = className+"."+ methodName;
        //确定当前操作是否是查询所有
        Class<?> returnType = method.getReturnType();
        if(returnType == List.class){
            //2）执行集合查询操作
            return sqlSession.selectList(key);
        } else {
            return sqlSession.selectOne(key, args);
        }

    }
}
